﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace daStaticAnalyser.Analysers
{
    using daStaticAnalyser;
    using daBase.Objects;
    using daBase.Analyser;
    using daSyntaxAnalyser;
    using daSyntaxAnalyser.SyntaxTree;

    [AnalyserDefaultSettings("Метрика Пивоварского", "test", 0, "test")]
    class PivovarskiAnalyzer : BaseStaticAnalyser
    {

        int GetData(EG node)
        {
            if (node == null) return 0;
            if (node.Last == null) return 0;
            else return GetData(node.Last)+1;
        }

        protected override void DoAnalyse(SyntaxTreeArgs e)
        {
            String MethodName = "";
            if (CheckMethod_GetName(e, out MethodName))
            {
                OuterMethodElement Method = e.Element as OuterMethodElement;
                ControlGraph CG = new ControlGraph();
                CG.Build(Method.Body.Statement);
                int Z = CG.Links.Count - CG.Elements.Count + 2;//Метрика МакКейба
                int S = 0;
                foreach (var node in CG.Elements)
                {
                    if (node is EGLink){}
                    else 
                    {
                        S += GetData(node);
                    }
                }
                int N = Z + S;
                AddMetric("Метрика Пивоварского", N,
                    MethodName,
                    Method.StartInfo.File,
                    Method.StartInfo.Line);
            }
        }
    }    
}
